package com.zm.tools.util.core.generatecode.common;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static com.zm.tools.util.core.generatecode.common.PackageNameUtil.getPackage;

/**
 * @author zoum
 * @create 2018/10/25 17:14
 */
public class GenerateModelCode {

    private final static Logger LOGGER = LoggerFactory.getLogger(GenerateModelCode.class);
    private final static String MODEL_NAME = "model";

    public static void generate(String baseFilePath, String tableName, String companyName, String appName) throws IOException{
        String pakage = getPackage(companyName, appName, MODEL_NAME);
        generateModel(baseFilePath, tableName, appName, MODEL_NAME, pakage);
        generateModelConvert(baseFilePath, tableName, appName, MODEL_NAME, pakage);
    }

    private static void generateModel(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        LOGGER.info("generateModel start for " + tableName);
        String model_vo_path = baseFilePath + "\\" + appName + "\\"+ appName + "-" + modelName +"\\src\\main\\java\\com\\rongzhijia\\member\\model\\vo";
        File model_vo_file  = new File(model_vo_path);
        if (!model_vo_file.exists()){
            model_vo_file.mkdir();
        }
        File vo = new File(model_vo_file.getPath(), tableName+"VO.java");
        if (vo.exists()){
            vo.delete();
        }
        vo.createNewFile();
        String dtoName = tableName + "DTO";
        FileWriter fw = new FileWriter(vo);
        StringBuilder sb = new StringBuilder();
        sb.append("package com.rongzhijia.member.model.vo;\n\n")
                .append("import com.rongzhijia.jiedianqian.basic.member.model.dto." + tableName.toLowerCase() + "." + dtoName + ";\n\n")
                .append("public class " + tableName + "VO extends " + dtoName + " {\n\n")
                .append("}");
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generateModel start for " + tableName);
    }

    private static void generateModelConvert(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        LOGGER.info("generateModelConvert start for " + tableName);
        String model_vo_path = baseFilePath + "\\" + appName + "\\"+ appName + "-" + modelName +"\\src\\main\\java\\com\\rongzhijia\\member\\model\\convert";
        File model_vo_file  = new File(model_vo_path);
        if (!model_vo_file.exists()){
            model_vo_file.mkdir();
        }

        String className = tableName+"Convert.java";
        File classFile = new File(model_vo_file.getPath(), className);
        if (classFile.exists()){
            classFile.delete();
        }
        classFile.createNewFile();
        String dtoName = tableName + "DTO";
        String voName = tableName + "VO";
        FileWriter fw = new FileWriter(classFile);
        StringBuilder sb = new StringBuilder();
        sb.append("package com.rongzhijia.member.model.convert;\n\n")
                .append("import com.rongzhijia.jiedianqian.basic.member.model.dto." + tableName.toLowerCase() + "." + dtoName + ";\n")
                .append("import com.rongzhijia.member.model.vo." + voName + ";\n")
                .append("import org.springframework.beans.BeanUtils;\n")
                .append("import java.util.ArrayList;\n")
                .append("import java.util.List;\n\n")
                .append("public class " + className.split("\\.")[0] + " {\n\n")
                .append("   public static " + dtoName + " toDTO("+ voName +" src" +"){\n")
                .append("       if(src == null){\n")
                .append("           return null;\n")
                .append("       }\n")
                .append("       "+dtoName + " target = new " + dtoName + "();\n")
                .append("       BeanUtils.copyProperties(src, target);\n")
                .append("       return target;\n")
                .append("   }\n")
                .append("   public static " + voName + " toVO("+ dtoName +" src" +"){\n")
                .append("       if(src == null){\n")
                .append("           return null;\n")
                .append("       }\n")
                .append("       "+voName + " target = new " + voName + "();\n")
                .append("       BeanUtils.copyProperties(src, target);\n")
                .append("       return target;\n")
                .append("   }\n")
                .append("   public static List<" + voName + "> toVOS(List<"+ dtoName +"> src" +"){\n")
                .append("       if(src == null || src.isEmpty()){\n")
                .append("           return null;\n")
                .append("       }\n")
                .append("       List<"+voName + "> target = new ArrayList<>();\n")
                .append("       src.forEach(dto -> target.add(toVO(dto)));\n")
                .append("       return target;\n")
                .append("   }\n")
                .append("}");
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generateModelConvert start for " + tableName);
    }
}
